|
1
|
|
|
// This is the master class, it does require the start code to be |
|
2
|
|
|
// included in the string |
|
3
|
|
|
class CODE128{ |
|
4
|
|
|
constructor(string){ |
|
5
|
|
|
// Fill the bytes variable with the ascii codes of string |
|
6
|
|
|
this.bytes = []; |
|
7
|
|
|
for (var i = 0; i < string.length; ++i) { |
|
8
|
|
|
this.bytes.push(string.charCodeAt(i)); |
|
9
|
|
|
} |
|
10
|
|
|
|
|
11
|
|
|
// First element should be startcode, remove that |
|
12
|
|
|
this.string = string.substring(1); |
|
13
|
|
|
|
|
14
|
|
|
// Data for each character, the last characters will not be encoded but are used for error correction |
|
15
|
|
|
// Numbers encode to (n + 1000) -> binary; 740 -> (740 + 1000).toString(2) -> "11011001100" |
|
16
|
|
|
this.encodings = [ // + 1000 |
|
17
|
|
|
740, 644, 638, 176, 164, 100, 224, 220, 124, 608, 604, |
|
18
|
|
|
572, 436, 244, 230, 484, 260, 254, 650, 628, 614, 764, |
|
19
|
|
|
652, 902, 868, 836, 830, 892, 844, 842, 752, 734, 590, |
|
20
|
|
|
304, 112, 94, 416, 128, 122, 672, 576, 570, 464, 422, |
|
21
|
|
|
134, 496, 478, 142, 910, 678, 582, 768, 762, 774, 880, |
|
22
|
|
|
862, 814, 896, 890, 818, 914, 602, 930, 328, 292, 200, |
|
23
|
|
|
158, 68, 62, 424, 412, 232, 218, 76, 74, 554, 616, |
|
24
|
|
|
978, 556, 146, 340, 212, 182, 508, 268, 266, 956, 940, |
|
25
|
|
|
938, 758, 782, 974, 400, 310, 118, 512, 506, 960, 954, |
|
26
|
|
|
502, 518, 886, 966, /* Start codes */ 668, 680, 692, |
|
27
|
|
|
5379 |
|
28
|
|
|
]; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
// The public encoding function |
|
32
|
|
|
encode() { |
|
33
|
|
|
var encodingResult; |
|
34
|
|
|
var bytes = this.bytes; |
|
35
|
|
|
// Remove the startcode from the bytes and set its index |
|
36
|
|
|
var startIndex = bytes.shift() - 105; |
|
37
|
|
|
|
|
38
|
|
|
// Start encode with the right type |
|
39
|
|
|
if(startIndex === 103){ |
|
40
|
|
|
encodingResult = this.nextA(bytes, 1); |
|
41
|
|
|
} |
|
42
|
|
|
else if(startIndex === 104){ |
|
43
|
|
|
encodingResult = this.nextB(bytes, 1); |
|
44
|
|
|
} |
|
45
|
|
|
else if(startIndex === 105){ |
|
46
|
|
|
encodingResult = this.nextC(bytes, 1); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
return { |
|
50
|
|
|
text: this.string.replace(/[^\x20-\x7E]/g, ""), |
|
51
|
|
|
data: |
|
52
|
|
|
// Add the start bits |
|
53
|
|
|
this.getEncoding(startIndex) + |
|
54
|
|
|
// Add the encoded bits |
|
55
|
|
|
encodingResult.result + |
|
|
|
|
|
|
56
|
|
|
// Add the checksum |
|
57
|
|
|
this.getEncoding((encodingResult.checksum + startIndex) % 103) + |
|
58
|
|
|
// Add the end bits |
|
59
|
|
|
this.getEncoding(106) |
|
60
|
|
|
}; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
getEncoding(n) { |
|
64
|
|
|
return (this.encodings[n] ? (this.encodings[n] + 1000).toString(2) : ''); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
// Use the regexp variable for validation |
|
68
|
|
|
valid() { |
|
69
|
|
|
// ASCII value ranges 0-127, 200-211 |
|
70
|
|
|
return this.string.search(/^[\x00-\x7F\xC8-\xD3]+$/) !== -1; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
View Code Duplication |
nextA(bytes, depth){ |
|
|
|
|
|
|
74
|
|
|
if(bytes.length <= 0){ |
|
75
|
|
|
return {"result": "", "checksum": 0}; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
var next, index; |
|
79
|
|
|
|
|
80
|
|
|
// Special characters |
|
81
|
|
|
if(bytes[0] >= 200){ |
|
82
|
|
|
index = bytes[0] - 105; |
|
83
|
|
|
|
|
84
|
|
|
// Remove first element |
|
85
|
|
|
bytes.shift(); |
|
86
|
|
|
|
|
87
|
|
|
// Swap to CODE128C |
|
88
|
|
|
if(index === 99){ |
|
89
|
|
|
next = this.nextC(bytes, depth + 1); |
|
90
|
|
|
} |
|
91
|
|
|
// Swap to CODE128B |
|
92
|
|
|
else if(index === 100){ |
|
93
|
|
|
next = this.nextB(bytes, depth + 1); |
|
94
|
|
|
} |
|
95
|
|
|
// Shift |
|
96
|
|
|
else if(index === 98){ |
|
97
|
|
|
// Convert the next character so that is encoded correctly |
|
98
|
|
|
bytes[0] = bytes[0] > 95 ? bytes[0] - 96 : bytes[0]; |
|
99
|
|
|
next = this.nextA(bytes, depth + 1); |
|
100
|
|
|
} |
|
101
|
|
|
// Continue on CODE128A but encode a special character |
|
102
|
|
|
else{ |
|
103
|
|
|
next = this.nextA(bytes, depth + 1); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
// Continue encoding of CODE128A |
|
107
|
|
|
else{ |
|
108
|
|
|
var charCode = bytes[0]; |
|
109
|
|
|
index = charCode < 32 ? charCode + 64 : charCode - 32; |
|
110
|
|
|
|
|
111
|
|
|
// Remove first element |
|
112
|
|
|
bytes.shift(); |
|
113
|
|
|
|
|
114
|
|
|
next = this.nextA(bytes, depth + 1); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
// Get the correct binary encoding and calculate the weight |
|
118
|
|
|
var enc = this.getEncoding(index); |
|
119
|
|
|
var weight = index * depth; |
|
120
|
|
|
|
|
121
|
|
|
return { |
|
122
|
|
|
"result": enc + next.result, |
|
123
|
|
|
"checksum": weight + next.checksum |
|
124
|
|
|
}; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
View Code Duplication |
nextB(bytes, depth){ |
|
|
|
|
|
|
128
|
|
|
if(bytes.length <= 0){ |
|
129
|
|
|
return {"result": "", "checksum": 0}; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
var next, index; |
|
133
|
|
|
|
|
134
|
|
|
// Special characters |
|
135
|
|
|
if(bytes[0] >= 200){ |
|
136
|
|
|
index = bytes[0] - 105; |
|
137
|
|
|
|
|
138
|
|
|
// Remove first element |
|
139
|
|
|
bytes.shift(); |
|
140
|
|
|
|
|
141
|
|
|
// Swap to CODE128C |
|
142
|
|
|
if(index === 99){ |
|
143
|
|
|
next = this.nextC(bytes, depth + 1); |
|
144
|
|
|
} |
|
145
|
|
|
// Swap to CODE128A |
|
146
|
|
|
else if(index === 101){ |
|
147
|
|
|
next = this.nextA(bytes, depth + 1); |
|
148
|
|
|
} |
|
149
|
|
|
// Shift |
|
150
|
|
|
else if(index === 98){ |
|
151
|
|
|
// Convert the next character so that is encoded correctly |
|
152
|
|
|
bytes[0] = bytes[0] < 32 ? bytes[0] + 96 : bytes[0]; |
|
153
|
|
|
next = this.nextB(bytes, depth + 1); |
|
154
|
|
|
} |
|
155
|
|
|
// Continue on CODE128B but encode a special character |
|
156
|
|
|
else{ |
|
157
|
|
|
next = this.nextB(bytes, depth + 1); |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
// Continue encoding of CODE128B |
|
161
|
|
|
else { |
|
162
|
|
|
index = bytes[0] - 32; |
|
163
|
|
|
bytes.shift(); |
|
164
|
|
|
next = this.nextB(bytes, depth + 1); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
// Get the correct binary encoding and calculate the weight |
|
168
|
|
|
var enc = this.getEncoding(index); |
|
169
|
|
|
var weight = index * depth; |
|
170
|
|
|
|
|
171
|
|
|
return {"result": enc + next.result, "checksum": weight + next.checksum}; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
View Code Duplication |
nextC(bytes, depth){ |
|
|
|
|
|
|
175
|
|
|
if(bytes.length <= 0){ |
|
176
|
|
|
return {"result": "", "checksum": 0}; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
var next, index; |
|
180
|
|
|
|
|
181
|
|
|
// Special characters |
|
182
|
|
|
if(bytes[0] >= 200){ |
|
183
|
|
|
index = bytes[0] - 105; |
|
184
|
|
|
|
|
185
|
|
|
// Remove first element |
|
186
|
|
|
bytes.shift(); |
|
187
|
|
|
|
|
188
|
|
|
// Swap to CODE128B |
|
189
|
|
|
if(index === 100){ |
|
190
|
|
|
next = this.nextB(bytes, depth + 1); |
|
191
|
|
|
} |
|
192
|
|
|
// Swap to CODE128A |
|
193
|
|
|
else if(index === 101){ |
|
194
|
|
|
next = this.nextA(bytes, depth + 1); |
|
195
|
|
|
} |
|
196
|
|
|
// Continue on CODE128C but encode a special character |
|
197
|
|
|
else{ |
|
198
|
|
|
next = this.nextC(bytes, depth + 1); |
|
199
|
|
|
} |
|
200
|
|
|
} |
|
201
|
|
|
// Continue encoding of CODE128C |
|
202
|
|
|
else{ |
|
203
|
|
|
index = (bytes[0] - 48) * 10 + bytes[1] - 48; |
|
204
|
|
|
bytes.shift(); |
|
205
|
|
|
bytes.shift(); |
|
206
|
|
|
next = this.nextC(bytes, depth + 1); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
// Get the correct binary encoding and calculate the weight |
|
210
|
|
|
var enc = this.getEncoding(index); |
|
211
|
|
|
var weight = index * depth; |
|
212
|
|
|
|
|
213
|
|
|
return {"result": enc + next.result, "checksum": weight + next.checksum}; |
|
214
|
|
|
} |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
export default CODE128; |
|
218
|
|
|
|